<template>
  <div class="app-container">

      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="卡券类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择" clearable filterable size="small" @change="selectChange" >
                <el-option
                  v-for="dict in typeOptions"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.key"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="卡券名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="卡券面额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入" >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="满额条件" prop="limitAmount">
              满
              <el-input style="width: 180px;" v-model="form.limitAmount" placeholder="请输入" >
              </el-input>
              元使用
              <el-checkbox style="margin-left: 20px;color: #282828;" size="mini" v-model="form.unlimitFlg">无门槛使用</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="有效时间" prop="time">
              <el-date-picker
                v-model="form.time"
                type="daterange"
                range-separator="至"
                :picker-options="pickerOptions"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="卡券种类" prop="kind">
              <el-radio v-for="(item,index) in kindOptions" v-model="form.kind" :label="item.key">虚拟券</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="会员限领" prop="count">
              <el-input v-model="form.count" placeholder="请输入" >
                <template slot="append">份</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="适用校区" prop="schoolIdArr">
              <el-select v-model="form.schoolIdArr" multiple placeholder="请选择">
                <el-option
                  v-for="dict in schoolOptions"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.key"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="适用人群" prop="userArr">
              <el-select v-model="form.userArr" multiple placeholder="请选择">
                <el-option
                  v-for="dict in userOptions"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.key"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="发放数量" prop="totalCount">
              <el-input style="width: 180px;" v-model="form.totalCount" placeholder="请输入" >
                <template slot="append">份</template>
              </el-input>
              <el-checkbox style="margin-left: 20px;color: #282828;" size="mini" v-model="form.unlimitTotalCountFlg">不限量</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="使用说明" prop="desc">
              <el-input style="width: 400px;" :rows="4" v-model="form.desc" type="textarea" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确定创建</el-button>
      </div>


  </div>
</template>

<script>
import { exportUser, } from "@/api/system/user";
import { listSchool } from "@/api/pt/school.js";
import { couponOption,addCoupon,editCoupon,oldCouponInfo } from "@/api/pt/coupon.js";

import { getToken } from "@/utils/auth";

export default {
  name: "addcoupon",
  components: {},
  data() {
    return {
      pickerOptions: {
        disabledDate(v) {
          return v.getTime() < new Date().getTime() - 86400000;
        }
      },
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 已有枚举
      schoolOptions: [],
      typeOptions: [],
      kindOptions: [],
      userOptions: [],
      // 表单参数
      form: {
        "amount": '',
        "count": '',
        "desc": "",
        "endTime": "",
        "kind": '1',
        "limitAmount": '',
        "name": "",
        "schoolIdArr": [],
        "startTime": "",
        "totalCount": '',
        "type": '1',
        "unlimitFlg": '',
        "unlimitTotalCountFlg": '',
        "userArr": [],
        time:[],
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        // limitAmount: [
        //   { required: true, message: "请输入", trigger: "blur" }
        // ],
        time: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
        kind: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
        count: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        schoolIdArr: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
        userArr: [
          { required: true, message: "请选择", trigger: "blur" }
        ],
        /* totalCount: [
          { required: true, message: "请输入", trigger: "blur" }
        ], */
        desc: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    if(this.$route.query.id){
      console.log(123)
      oldCouponInfo(this.$route.query.id).then(response => {
        console.log(response.data)
        this.form = response.data
        this.form.schoolIdArr.map((item,index)=>{
          this.form.schoolIdArr[index] = JSON.stringify(item)
        })
        this.form.userArr.map((item,index)=>{
          this.form.userArr[index] = JSON.stringify(item)
        })
        // this.form.time = [new Date(this.form.startTime),new Date(this.form.endTime)]
        this.form.startTime = ''
        this.form.endTime = ''
        this.form.unlimitFlg = this.form.unlimitFlg==1?true:false
        this.form.unlimitTotalCountFlg = this.form.unlimitTotalCountFlg==1?true:false
        this.form.kind = JSON.stringify(this.form.kind)
      });
    }
    couponOption().then(response => {
      this.schoolOptions = response.data.school
      this.typeOptions = response.data.type
      this.kindOptions = response.data.kind
      this.userOptions = response.data.user
    })
  },
  methods: {
    // 取消按钮
    cancel() {
    },

    /** 提交按钮 */
    submitForm: function() {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.time){
            this.form.startTime =  this.parseTime(this.form.time[0])
            this.form.endTime =  this.parseTime(this.form.time[1], '{y}-{m}-{d} {23}:{59}:{59}')
          }
          let pa = {...this.form}
          if(!this.form.limitAmount && !this.form.unlimitFlg){
            this.msgInfo("请填写满额条件或勾选无门槛");
            return
          }

          if(!this.form.totalCount && !this.form.unlimitTotalCountFlg){
            this.msgInfo("请填写发放数量或勾选不限额");
            return
          }
          delete pa.time
          pa.schoolIdArr = pa.schoolIdArr.join(',')
          pa.userArr = pa.userArr.join(',')
          pa.unlimitFlg =	pa.unlimitFlg?1:0
          pa.unlimitTotalCountFlg = pa.unlimitTotalCountFlg?1:0
          if (pa.id !== undefined) {
            editCoupon(pa.id,pa).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.message);
              }
            });
          } else {
            pa.id = undefined
            addCoupon(pa).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
              } else {
                this.msgError(response.message);
              }
            });
          }
        }
      });
    },
    // 获取当前
    selectChange(val) {
    },
  }
};
</script>

<style type="text/css">
  .dian {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    display: inline-block;
    background: #999;
    margin-right: 6px;
  }
</style>
